<?php

namespace Sharks\Core\Http;

use InvalidArgumentException;
use Sharks\Core\Support\Base\ObjPropsReadOnly;
use Sharks\Core\Support\Base\PropertiesReadOnly;

/**
 * 封装访问的url
 * 各属性值参见http协议中关于url的构成,或参考php的 parse_url 函数
 */
class Url {

    use PropertiesReadOnly;

    private static $READ_ONLY = array("value", "host", "scheme", "port", "user", "pass", "path", "query", "hash");

    private $value;
    private $host;
    private $scheme;
    private $port;
    private $user;
    private $pass;
    private $path;
    private $query;
    private $hash;

    public function __construct($url = null) {
        if (is_null($url)) {
            
            $this->host = $_SERVER['HTTP_HOST'];
            $this->scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
            $this->port = $_SERVER['SERVER_PORT'];
            $this->user = null;
            $this->pass = null;
            $this->path = $this->initPath();
            if(isset($_SERVER[ 'QUERY_STRING' ])) {
                parse_str($_SERVER[ 'QUERY_STRING' ], $this->query);
            }
            else {
                $this->query = array();
            }
            $this->hash = null;
            $this->value = "$this->scheme://$this->host$this->path";

        }
        else {
            $resolveResult = parse_url($url);

            if (!$resolveResult) {
                throw new InvalidArgumentException("错误的URL:$url");
            }

            $this->value = $url;
            $this->host = $_SERVER[ 'HTTP_HOST' ];
            $this->scheme = isset($resolveResult['schema']) ? $resolveResult['schema'] : null;
            $this->port = isset($resolveResult['port']) ? $resolveResult['port'] : null;
            $this->user = isset($resolveResult['user']) ? $resolveResult['user'] : null;
            $this->pass = isset($resolveResult['pass']) ? $resolveResult['pass'] : null;
            $this->path = isset($resolveResult['path']) ? $resolveResult['path'] : '/';
            if(isset($resolveResult['query'])) {
                parse_str($resolveResult['query'], $this->query);
            }
            else {
                $this->query = array();
            }
            $this->hash = isset($resolveResult['fragment']) ? $resolveResult['fragment'] : null;
        }
    }

    /**
     * 初始化path
     * @return string
     */
    protected function initPath() {
        if (isset($_SERVER['PATH_INFO'])) {
            $path = $_SERVER['PATH_INFO'];
        }
        else {
            if (isset($_SERVER['REQUEST_URI']) && !empty($_SERVER['REQUEST_URI'])) {
                if (false === strpos($_SERVER['REQUEST_URI'], '?')) {
                    $path = $_SERVER['REQUEST_URI'];
                }
                else {
                    $path =  explode('?', $_SERVER['REQUEST_URI'])[0];//移除查询字符串
                }
            }
            else {
                $path = '/';
            }
        }

        if ('/' != $path && '/' == substr($path, -1)) {
            $path = rtrim($path, '/');
        }

        if ('' == $path) {
            $path = '/';
        }

        return $path;
    }
}